package com.sky.jtail.conf;

import com.sky.jtail.domain.AppConstant;
import com.sky.jtail.domain.AppContainer;
import com.sky.jtail.utils.SpringBeanUtil;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class LoginInterceptor implements HandlerInterceptor {

    /**
     * 在请求处理之前进行调用（Controller方法调用之前）
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {

        try {
            //统一拦截
            String auth =(String)request.getSession().getAttribute(AppConstant.webAuth);
            if(auth!=null){
                AppContainer appContainer = (AppContainer) SpringBeanUtil.getBean("appContainer");
                if(appContainer.authValues.containsKey(auth)){
                    return true;
                }

            }
            response.sendRedirect(request.getContextPath()+"login.html");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }
 

    
}